package com.dormitory.myt.sushecontroller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dormitory.myt.common.R;
import com.dormitory.myt.en.Allsushe;
import com.dormitory.myt.en.Detail;
import com.dormitory.myt.en.Rate;
import com.dormitory.myt.mapper.AllsusheMapper;
import com.dormitory.myt.mapper.RateMapper;
import com.dormitory.myt.service.AllsusheService;
import com.dormitory.myt.service.RateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/rate")
@Slf4j
public class RateController {
    @Autowired
    private RateService rateService;
    @Autowired
    private RateMapper rateMapper;
    @Autowired
    private AllsusheMapper allsusheMapper;
    @Autowired
    private AllsusheService allsusheService;

    /**
     * 评价
     * @param response
     * @param sushehao
     * @param rate
     * @return
     */
    @GetMapping
    public R<String> r(HttpServletResponse response, @RequestParam Integer sushehao, @RequestParam Integer rate){
        response.setHeader("Access-Control-Allow-Origin","*");

        Allsushe allsushe = new Allsushe();
        allsushe.setSushehao(sushehao);
        allsushe.setRate(rate);
        LambdaUpdateWrapper<Allsushe> updateWrapper = Wrappers.<Allsushe>lambdaUpdate()
                .eq(Allsushe::getSushehao, allsushe.getSushehao());
        allsusheMapper.update(allsushe, updateWrapper);

        if(rate==1||rate==5) {
            log.info("加入1或者5序列");
            //判断是否已经添加了：
            LambdaQueryWrapper<Rate> queryWrapper = new LambdaQueryWrapper<>();
            //如果已经有了就根据宿舍号修改:
            queryWrapper.eq(Rate::getSushehao, sushehao);
            List<Rate> list = rateService.list(queryWrapper);
            if (list != null && !list.isEmpty()) {
                Rate r = list.get(0);
                // 处理查询结果
                log.info("查出来的宿舍评价：{}",r);
                    r.setRate(rate);
                log.info("修改后的宿舍评价：{}",r);
                    LambdaUpdateWrapper<Rate> updateWrapper2 = Wrappers.<Rate>lambdaUpdate()
                            .eq(Rate::getSushehao, r.getSushehao());
                    rateMapper.update(r, updateWrapper2);
            } else {
                //如果没有则直接插入:

                if (rate == 1) {
                    Rate r2 = new Rate();
                    r2.setRate(rate);
                    r2.setSushehao(sushehao);
                    rateService.save(r2);
                } else if (rate == 5) {
                    Rate r2 = new Rate();
                    r2.setRate(rate);
                    r2.setSushehao(sushehao);
                    rateService.save(r2);
                }
            }


        }

        return R.success("year");
    }


    /**
     * 初始查询所有的黑名单或者优秀宿舍
     * @param response
     * @param currentPage
     * @param pageSize
     * @param rate
     * @return
     */
    @GetMapping("/selectRate")
    public R<Page> r2(HttpServletResponse response, @RequestParam int currentPage,@RequestParam int pageSize,@RequestParam Integer rate){
        response.setHeader("Access-Control-Allow-Origin","*");
        //分页构造器
        Page<Rate> pageInfo = new Page<>(currentPage,pageSize);
        //条件构造器
        LambdaQueryWrapper<Rate> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Rate::getRate,rate);
        //分页查询
        rateService.page(pageInfo,queryWrapper);
        return R.success(pageInfo);

    }

    /**
     * 分页查询：
     * @param response
     * @param currentPage
     * @param pageSize
     * @param rate
     * @return
     */
    @GetMapping("/selectRate2")
    public R<Page> r3(HttpServletResponse response, @RequestParam int currentPage,@RequestParam int pageSize,Integer rate){
        response.setHeader("Access-Control-Allow-Origin","*");
        //分页构造器
        Page<Rate> pageInfo = new Page<>(currentPage,pageSize);
        //条件构造器
        LambdaQueryWrapper<Rate> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Rate::getRate,rate);
        //分页查询
        rateService.page(pageInfo,queryWrapper);
        return R.success(pageInfo);

    }
    /**
     * 根据宿舍号删除评价
     * @param response
     * @param sushehao
     * @return
     */
    @GetMapping("/delete")
    public R<String> r1(HttpServletResponse response, @RequestParam String sushehao){
        response.setHeader("Access-Control-Allow-Origin","*");
        rateService.removeById(sushehao);
        return R.success("year");
    }
}

